﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using Reviewer.Data.Bases;
using Reviewer.Entities;
using Reviewer.Utilities.Data;
using System.Globalization;

namespace Reviewer.Data
{
    /// <summary>
    /// Distributor repository
    /// </summary>
    [CLSCompliant(false)]
    public class DistributorRepository : RepositoryCrudBase<Distributor, Int32>
    {
        #region fields (static)
        private const string TABLE_NAME = "V2_Distributor";
        private static readonly CrudColumnInfo columnId = new CrudColumnInfo(0, "Id", MySqlDbType.Int32, true, false, false, true);
        private static readonly CrudColumnInfo columnName = new CrudColumnInfo(1, "Navn", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnPhone = new CrudColumnInfo(2, "Telefon", MySqlDbType.VarChar, 255, false, false, true);
        #endregion

        #region methods (public)
        #endregion

        #region methods (protected)
        internal override CrudColumnInfo[] Columns
        {
            get
            {
                return new CrudColumnInfo[]{
                    DistributorRepository.columnId
                    , DistributorRepository.columnName
                    , DistributorRepository.columnPhone
                };
            }
        }

        internal override object ColumnToValue(CrudColumnInfo info, Distributor item)
        {
            if (info != null && item != null)
            {
                if (info.Equals(DistributorRepository.columnId))
                    return item.Id;
                else if (info.Equals(DistributorRepository.columnName))
                    return item.Name;
                else if (info.Equals(DistributorRepository.columnPhone))
                    return item.Phone;
            }
            return null;
        }

        protected override string TableName
        {
            get { return DistributorRepository.TABLE_NAME; }
        }

        protected override Distributor BuildItemFromReader(System.Data.IDataReader reader)
        {
            Distributor item = new Distributor();
            if (reader != null)
            {
                item.Id = DataReaderUtility.GetValue(reader, reader.GetOrdinal(DistributorRepository.columnId.Name), 0);
                item.Name = DataReaderUtility.GetValue(reader, reader.GetOrdinal(DistributorRepository.columnName.Name), String.Empty);
                item.Phone = DataReaderUtility.GetValue(reader, reader.GetOrdinal(DistributorRepository.columnPhone.Name), String.Empty);
            }
            return item;
        }
        #endregion
    }
}
